package com.ebupt.migu.music.messageCenter.controller;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.CSVUtils;
import com.ebupt.migu.music.common.util.DateUtil;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.messageCenter.service.MessageCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2020/3/6
 */
@RestController
@RequestMapping("/message-center")
public class MessageCenterController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private MessageCenterService messageCenterService;
    @Autowired
    private IDAuthUtils idAuthUtils;
    /**
     * 图片保存路径
     */
    @Value("${path.push_image_path}")
    private String pushImagePath;

    /**
     * 添加通知类型
     *
     * @return
     */
    @RequestMapping("/add-notice-type")
    public ResultObject addNoticeType() {
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return messageCenterService.addNoticeType(params);
    }

    /**
     * 查询消息通知列表
     *
     * @return
     */
    @RequestMapping("/query-notice-list")
    public ResultObject queryNoticeList() {
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return messageCenterService.queryNoticeList(params);
    }

    /**
     * 更新通知类型
     *
     * @return
     */
    @RequestMapping("/update-notice-type")
    public ResultObject updateNoticeType() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdmin(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权操作此通知类型");
        }
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return messageCenterService.updateNoticeType(params);
    }

    /**
     * 上传发送者头像
     *
     * @return
     */
    @RequestMapping(value = "/icon")
    public ResultObject imageUpload(@RequestParam("file") MultipartFile file) {
        ResultObject res = new ResultObject();
        Date date = new Date();
        String timeStr = DateUtil.parseDateToStr(date, DateUtil.DATE_TIME_FORMAT_YYYYMMDDHHMISS) + ".png";
        String newPath = pushImagePath + timeStr;
        //上传图片到指定目录
        CSVUtils.upload(file, newPath);
        Map<String, Object> retMap = new HashMap<String, Object>();
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        retMap.put("icon", newPath);
        retMap.put("iconReName", timeStr);
        res.setData(retMap);
        return res;
    }

    /**
     * 消息推送--图片删除
     *
     * @return
     */
    @RequestMapping(value = "/delete-icon")
    public ResultObject imageDel() {
        ResultObject res = new ResultObject();
        Map<String, Object> paramMap = ReqestParamUtil.getParams(request);
        String path = paramMap.get("icon").toString();
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        res.setMsg("删除图片成功");
        return res;
    }

}
